// packageI/invoice/components/balance_invoice_List/balance_invoice_List.js
const app = getApp();
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: null,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    catchBtnTap(e) {
      console.log(e)
      let {
        items,
        id
      } = e.currentTarget.dataset;
      console.log(items, id);
      this.data.rid = id;
      if (items.value == "open") {
        this.openMethods(items);
      } else {
        this.to_detail(items, id);
      }
    },
    openMethods(items) {
      wx.showModal({
        content: `您确定要${items.name}吗?`,
        complete: async (res) => {
          if (res.confirm) {
            let {
              data
            } = await app._postNetWork({
              url: app.getNetAddresss("plugin.balance-invoice.frontend.invoice-set.index"),
            })
            if (data.result) {
              this.setData({
                api: items.api,
              })
              this.selectComponent('.invoicePop').balance_invoice_init({
                sinvoice: data.data
              });
            } else {
              app.tips(data.msg);
            }
          }
        }
      })
    },
    to_detail(items, rid) {
      wx.navigateTo({
        url: `/packageI/invoice/invoice-details/invoice-details?id=${rid}&tag=balance_invoice`,
      })
    },
    async subInvoice(e) {
      let json = e.detail;
      json.rid = this.data.rid;
      console.log(json, "发票组件回传数据");
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss(this.data.api),
        data: json
      })
      if (data.result) {
        app.tips(data.msg);
        this.triggerEvent("update")
      } else {
        app.tips(data.msg);
      }
    },
    invoiceShowAddressFun(){
      this.setData({
        showAddressList: true,
        addressList_caller:"invoice"
      });
    },
    confirmSelectAddress(e) {
      let info;
      if (e.detail.loc) {
        info = e.detail.form;
      } else {
        info = e.detail;
      }
      if(this.data.addressList_caller == "invoice") {
        this.selectComponent('.invoicePop').setAddress(info);
        this.setData({
          showAddressList: false,
        });
      }
    },
  }
})